/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.param.ShopAuditingParam;
import com.yami.shop.bean.dto.ShopAuditingInfoDto;
import com.yami.shop.bean.enums.AuditStatus;
import com.yami.shop.bean.enums.OfflineHandleEventType;
import com.yami.shop.bean.enums.ShopStatus;
import com.yami.shop.bean.model.OfflineHandleEvent;
import com.yami.shop.bean.model.ShopAuditing;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.param.AuditingInfoParam;
import com.yami.shop.bean.param.OfflineHandleEventAuditParam;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.OfflineHandleEventService;
import com.yami.shop.service.ShopAuditingService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.sys.comment.service.ShopEmployeeService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;


/**
 * 商家审核信息
 *
 * @author Dwl
 * @date 2019-09-19 14:02:57
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/shopAuditing")
public class ShopAuditingController {

    private final ShopAuditingService shopAuditingService;
    private final ShopDetailService shopDetailService;
    private final OfflineHandleEventService offlineHandleEventService;
    private final ShopEmployeeService shopEmployeeService;
    /**
     * 分页查询
     *
     * @param page              分页对象
     * @param auditingInfoParam 商家审核信息
     * @return 分页数据
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('shop:shopAuditing:page')")
    public ResponseEntity<IPage<ShopAuditingInfoDto>> getShopAuditingPage(PageParam<ShopAuditingInfoDto> page, AuditingInfoParam auditingInfoParam) {
        return ResponseEntity.ok(shopAuditingService.auditingInfoList(page, auditingInfoParam));
    }

    /**
     * 根据审核id查询详情信息
     */
    @GetMapping("/shopDetail/{shopId}")
    @PreAuthorize("@pms.hasPermission('shop:shopAuditing:info')")
    public ResponseEntity<ShopDetail> auditingDetail(@PathVariable Long shopId) {
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        return ResponseEntity.ok(shopDetail);
    }

    /**
     * 根据审核id查询详情信息
     */
    @GetMapping("/{shopId}")
    public ResponseEntity<ShopAuditing> getShopAuditing(@PathVariable Long shopId) {
        ShopAuditing shopAuditing = shopAuditingService.getOne(new LambdaQueryWrapper<ShopAuditing>().eq(ShopAuditing::getShopId, shopId));
        if (Objects.isNull(shopAuditing)){
            shopAuditing = new ShopAuditing();
        }
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        shopAuditing.setShopDetail(shopDetail);
        return ResponseEntity.ok(shopAuditing);
    }

    /**
     * 审核信息 第一次审核商家
     */
    @SysLog("审核商家信息")
    @PutMapping("/audit/{shopId}")
    @PreAuthorize("@pms.hasPermission('shop:shopAuditing:audit')")
    public ResponseEntity<Void> audit(@PathVariable Long shopId, @Valid @RequestBody ShopAuditingParam shopAuditingParam) {

        ShopAuditing shopAuditing = shopAuditingService.getOne(new LambdaQueryWrapper<ShopAuditing>().eq(ShopAuditing::getShopId, shopId));
        if (Objects.equals(shopAuditing.getStatus(), AuditStatus.SUCCESSAUDIT.value())) {
            // 商家已经审核成功，无需再次审核
            throw new YamiShopBindException("yami.stop.examine.success");
        }
        shopAuditing.setUpdateTime(new Date());
        shopAuditing.setRemarks(shopAuditingParam.getRemarks());
        shopAuditing.setStatus(shopAuditingParam.getStatus());
        shopAuditing.setAuditorId(SecurityUtils.getSysUser().getUserId());
        shopDetailService.audit(shopAuditing);

        shopDetailService.removeShopDetailCacheByShopId(shopId);

        return ResponseEntity.ok().build();
    }

    /**
     * 新建店铺
     */
    @PostMapping
    public ResponseEntity<ShopDetail> insertDetail(@RequestBody ShopDetail shopDetail) {
        // 判断是账号是否已存在
        int count = shopEmployeeService.checkUserName(shopDetail.getShopId(), shopDetail.getMobile());
        shopDetailService.insertDetail(shopDetail,count);
        return ResponseEntity.ok(shopDetail);
    }

    /**
     * 校验账号
     */
    @GetMapping("/checkMobile")
    public ResponseEntity<Boolean> checkMobile(@RequestParam("mobile") String mobile,@RequestParam("shopId")Long shopId) {
        Boolean isTrue = true;
        if (Objects.isNull(shopId)){
            shopId = 0L;
        }
        int count = shopDetailService.checkMobile(mobile,Long.valueOf(shopId));
        // 加多了一个表，并且为了适配，保证数据的唯一性，需要同时验证两个表的数据
        count = count + shopEmployeeService.checkUserName(shopId, mobile);
        if (count > 0){
            isTrue = false;
        }
        return ResponseEntity.ok(isTrue);
    }


    /**
     * 重置密码或者修改账号
     */
    @PutMapping("/updatePasswordOrMobile")
    public ResponseEntity<Boolean> updatePasswordOrMobile(@RequestBody ShopDetail shopDetail) {
        // 判断是账号是否已存在
        int count = shopEmployeeService.checkUserName(shopDetail.getShopId(), shopDetail.getMobile());
        shopDetailService.updatePasswordOrMobile(shopDetail.getShopId(),shopDetail.getPassword(),shopDetail.getMobile(),count);
        return ResponseEntity.ok().build();
    }

//    /**
//     * 下线店铺
//     */
//    @SysLog("下线店铺信息")
//    @PutMapping("/lowerShelfShop")
//    @PreAuthorize("@pms.hasPermission('shop:shopAuditing:lowerShelfShop')")
//    public ResponseEntity<Boolean> lowerShelfShop(@RequestBody OfflineHandleEvent offlineHandleEvent) {
//        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(offlineHandleEvent.getHandleId());
//        if (shopDetail == null) {
//            throw new YamiShopBindException("店铺信息不存在");
//        }
//        if (!Objects.equals(shopDetail.getShopStatus(), ShopStatus.OPEN.value())) {
//            throw new YamiShopBindException("店铺不处于营业状态,不能进行下线");
//        }
//        shopDetail.setShopStatus(ShopStatus.STOP.value());
//        return ResponseEntity.ok(shopDetailService.updateById(shopDetail));
//    }


}
